package com.website.web.back.controller.website;

import com.website.common.annotation.Log;
import com.website.common.core.controller.BaseController;
import com.website.common.core.domain.AjaxResult;
import com.website.common.core.page.TableDataInfo;
import com.website.common.enums.BusinessType;
import com.website.common.utils.StringUtils;
import com.website.common.utils.poi.ExcelUtil;
import com.website.custom.domain.WCase;
import com.website.custom.service.IWCaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 案例展示Controller
 * 
 * @author ruoyi
 * @date 2024-12-03
 */
@Controller
@RequestMapping("/back/custom/case")
public class WCaseController extends BaseController
{
    private String prefix = "back/website/case";

    @Autowired
    private IWCaseService wCaseService;

    @RequiresPermissions("system:case:view")
    @GetMapping()
    public String carousel()
    {
        return prefix + "/case";
    }


    /**
     * 查询案例展示列表
     */
    @RequiresPermissions("system:case:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WCase wCase)
    {
        startPage();
        List<WCase> list = wCaseService.selectWCaseList(wCase);
        return getDataTable(list);
    }

    /**
     * 导出案例展示列表
     */
    @RequiresPermissions("system:case:export")
    @Log(title = "案例展示", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WCase wCase)
    {
        List<WCase> list = wCaseService.selectWCaseList(wCase);
        ExcelUtil<WCase> util = new ExcelUtil<WCase>(WCase.class);
        return util.exportExcel(list, "案例展示数据");
    }

    /**
     * 新增案例展示
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存案例展示
     */
    @RequiresPermissions("system:case:add")
    @Log(title = "案例展示", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WCase wCase)
    {
        Long maxId = wCaseService.getMaxId();
        wCase.setPrewId(maxId);
        wCaseService.insertWCase(wCase);
        // 更新上一条新闻信息
        WCase prewCase = wCaseService.selectWCaseById(maxId);
        prewCase.setNextId(wCase.getId());
        wCaseService.updateWCase(prewCase);
        return toAjax(1);
    }

    /**
     * 修改案例展示
     */
    @RequiresPermissions("system:case:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WCase wCase = wCaseService.selectWCaseById(id);
        mmap.put("wCase", wCase);
        return prefix + "/edit";
    }

    /**
     * 修改保存案例展示
     */
    @RequiresPermissions("system:case:edit")
    @Log(title = "案例展示", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WCase wCase)
    {
        if (StringUtils.isEmpty(wCase.getImg())) {
            WCase wCase1 = wCaseService.selectWCaseById(wCase.getId());
            String img = wCase1.getImg();
            if (!StringUtils.isEmpty(img)) {
                wCase.setImg(img);
            }
        }
        return toAjax(wCaseService.updateWCase(wCase));
    }

    /**
     * 删除案例展示
     */
    @RequiresPermissions("system:case:remove")
    @Log(title = "案例展示", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wCaseService.deleteWCaseByIds(ids));
    }
}
